package main

import (
	"fmt"
	"sync"
	"time"
)

var coreWg sync.WaitGroup

func main() {
	var wg sync.WaitGroup
	wg.Add(2)
	say2("hello", &wg)
	say2("world", &wg)
	fmt.Println("over!")

	for i := 0; i < 3; i++ {
		coreWg.Add(1)
		go coreWorker(i)
	}
	coreWg.Wait()
	fmt.Println("All workers have finished.")
}

func coreWorker(id int) {
	defer coreWg.Done()
	fmt.Printf("Worker %d is starting\n", id)
	time.Sleep(2 * time.Second)
	fmt.Printf("Worker %d has finished\n", id)
}
func say2(s string, waitGroup *sync.WaitGroup) {
	defer waitGroup.Done()
	for i := 0; i < 3; i++ {
		fmt.Println(s)
	}
}
